﻿
$.MGJ.CodeRelate = [];

(function () {
    var _this = $.MGJ.CodeRelate;
    $.extend($.MGJ.CodeRelate, {
            PageIndex: 1,
            PageSize: 10,
            searchKey: null,//查询条件
            action:0,
            Ajax_GetCodeRelatePageList: function (pageIndex, action) {
            this.action = action || this.action;
            _this.PageIndex = pageIndex || 1;
            var searchKey = $("#SearchKey").val();
            $.L.ajaxHandle({
                url: ACTION_URL("GetCodeRelatePageList", "CodeManage"),
                data: {
                    searchKey: searchKey,
                    PageIndex: _this.PageIndex,
                    pageSize: _this.PageSize,
                    action: this.action
                },
                success: function (response) {
                    $("#PageList").L_bindingDataList(response);
                    $(".StartCode").each(function () {

                        if ($(this).text() == 0) {
                            $(this).html("")
                        }                      

                    })
                    $(".EndCode").each(function () {

                        if ($(this).text() == 0) {
                            $(this).html("")
                        }

                    })
                    $(".RelateCodeNums").each(function () {

                        if ($(this).text() == 0) {
                            $(this).html("整批")
                        }

                    })

                  
                },
               
            });
        },

        //操作栏的启用
        StatsButton: function (a, b, c) {
        
            if (0 == a) {

                var a = "<a style='color:#ccc;cursor:text;text-decoration:none;'>已启用</a>"
                $(c[0]).append(a)

            } else if (1 == a) {
                a = "<a rid=" + b.ID + " StartCode=" + b.StartCode + " CodeBatch=" + b.CodeBatch + " EndCode=" + b.EndCode + " RelateType=" + b.RelateType + " WL_CodeDisable=" + b.WL_CodeDisable + " Action=" + b.Action + " onclick='$.MGJ.CodeRelate.Ajax_CodeDisableflag(this)'>启用</a>"
                $(c[0]).append(a)

            }
        },
        Ajax_Stats: function (obj) {
        
            return obj == 0 ? "启用" : "禁用";
        },

      

        Ajax_CodeDisableflag: function (a) {       
            $.L.msgConfirm("是否启用该码?", function () {
                var id = a.attributes.rid.nodeValue;
                var scode = a.attributes.startcode.nodeValue;
                var ecode = a.attributes.endcode.nodeValue;
                var batch = a.attributes.codebatch.nodeValue;
                var type = a.attributes.relatetype.nodeValue;
                var wl_codedisable = a.attributes.wl_codedisable.nodeValue;
                var action = a.attributes.action.nodeValue;
                $.L.ajaxHandle({
                    url: ACTION_URL("CodeDisableflag", "CodeManage"),
                    data: { id: id, scode: scode, ecode: ecode, batch: batch, type: type, wl_codedisable: wl_codedisable, action: action },
                    success: function (response) {
                        
                        if (response > 0) {
                            _this.Ajax_GetCodeRelatePageList(1, 1);
                            $.L.msgSuccess("已启用！");
                        }
                        else {
                            $.L.msgWarning("请检查该批码是否已启用！");
                        }
                    }

                });
            })

        },
        AllotTypeName: function (val) {
            if(val==0){
                return "按号段";
            }
            if(val==1){
                return "整批";
            }
            if (val == 2) {
                return "按单号";
            }
            //按整版分配 计算总数
            if (allotType == 1) {
                _this.ReturnSelectCount(1);
            }
            if (allotType == 2)
            {
                _this.ReturnSelectCount(2);
            }
        },
        //设置分配方式
        SetAllotType: function (a) {
            _this.allotType = a;
        },
        allotType: 0,
        onBatchChange: function () {
            var type = _this.allotType;
            _this.ReturnSelectCount(type);
        },

        //计算已选数量
        ReturnSelectCount: function (type) {
            var _batchNum = $("#CodeBatch").find("option:selected").text();
            var wlcode = $("#BoxCodes").val();
            if (_batchNum) {
                $.L.ajaxHandle({
                    url: ACTION_URL("GetWholeBatchCount", "CodeManage"),
                    type: "GET",
                    data: { batchNo: _batchNum, wlcode: wlcode },
                    success: function (r) {
                        $("#HidCount").val(r);
                        if (type == 0) {
                            var _startCode = Number($("#StartCode").val());
                            var _endCode = Number($("#EndCode").val());
                            if (_startCode != "" && _endCode != "") {
                                $("#RelateCodeNums1").val(parseInt(_endCode) - parseInt(_startCode) + 1);
                            }
                        }
                        else if (type == 1) {
                            $("#RelateCodeNums2").val(r);
                        }
                    }
                })
            }
            //if (type == 0) {
            //    var _startCode = Number($("#StartCode").val());
            //    var _endCode = Number($("#EndCode").val());
            //    if (_startCode <= 0) {
            //        $("#StartCode").val(0);
            //    }
            //    if (_endCode <= 0) {
            //        $("#EndCode").val(0);
            //    }
            //    if (_endCode >= _startCode) {
            //        $("#RelateCodeNums1").val((_endCode - _startCode));
            //    } else {
            //        $("#RelateCodeNums1").val(0);
            //    }
            //} else if (type == 1) {
            //    var _batchNum = $("#CodeBatch").find("option:selected").text();
            //    if (_batchNum) {
            //        $.L.ajaxHandle({
            //            url: ACTION_URL("GetWholeBatchCount", "CodeManage"),
            //            type: "GET",
            //            data: { batchNo: _batchNum },
            //            success: function (r) {
                         
            //                $("#RelateCodeNums2").val(r);
            //            }
            //        })
            //    }
            //}
            //else if (type == 2) {
            //    var wlcode = $("#BoxCodes").val();
            //    if (wlcode) {
            //        $.L.ajaxHandle({
            //            url: ACTION_URL("GetWholeBatchCount", "CodeManage"),
            //            type: "GET",
            //            data: { wlcode: wlcode },
            //            success: function (r) {
                         
            //                $("#RelateCodeNums3").val(r);
            //            }
            //        })
            //    }
            //}
        },


    
        Dialog_CodeRelateAddObj: null,
        Dialog_CodeRelateAdd: function (obj) {

            this.Dialog_CodeRelateAddObj = $.L.dialog({
                url: ACTION_URL("CodeRelateAdd", "CodeManage"),
                title: "码关联",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#CodeRelateForm").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_CodeRelateSave();
                                $(".btn-primary").attr("disabled", true);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            }
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },

        Dialog_CodeDisableAddObj: null,
        Dialog_CodeDisableAdd: function (obj) {
 
            this.Dialog_CodeDisableAddObj = $.L.dialog({
                url: ACTION_URL("CodeDisableAdd", "CodeManage"),
                title: "码禁用",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#CodeRelateForm").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_CodeDisplaySave();
                                $(".btn-primary").attr("disabled", true);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            }
                            
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
       
        Dialog_CodeDisableCheckObj: null,
        Dialog_CodeDisableCheck: function (obj) {
            this.Dialog_CodeDisableCheckObj = $.L.dialog({
                url: ACTION_URL("CodeDisableCheck", "CodeManage"),
                title: "查看详情",
                width: 800,
                height: "auto",
                buttons: {
                    //"button_save": {
                    //    type: 'primary',
                    //    text: '保存',
                    //    click: function () {
                    //        alert('查看详情');
                    //    }
                    //},
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
            var RelateType = $(obj).parent().parent().find('.RelateType').text();
            var CodeBatch = $(obj).parent().parent().find('.CodeBatch').text();
            var StartCode = $(obj).parent().parent().find('.StartCode').text();
            var EndCode = $(obj).parent().parent().find('.EndCode').text();
            var RelateCodeNums = $(obj).parent().parent().find('.RelateCodeNums').text();
            var BoxCodes = $(obj).parent().parent().find(".BoxCodes").text()
            setTimeout(function () {
                if (RelateType == '按号段') {
                    $("#para").attr("checked", true).removeAttr("style");
                    $("label[for='all']").hide();
                    $("label[for='singleCode']").hide();
                    $("#codebatchs").show();
                    $("#SectionNum").show();
                    $("#single_code").hide();
                    $("#codebatchs").find("input").val(CodeBatch).attr("disabled", true);
                    $("#StartCode").val(StartCode).attr("disabled", true);
                    $("#EndCode").val(EndCode).attr("disabled", true);
                    $("#SectionNum2").find("input").attr({ "id": "RelateCodeNums1", "disabled": true }).val(RelateCodeNums);
                }
                else if (RelateType == '按单号') {
                    $("#singleCode").attr("checked", true).removeAttr("style");
                    $("label[for='para']").hide();
                    $("label[for='all']").hide();
                    $("#single_code").show();
                    $("#SectionNum").hide();
                    $("#codebatchs").hide();
                    $("#BoxCodes").val(BoxCodes).attr("disabled", true).css("margin-left",0);
                    $("#SectionNum2").find("input").attr({ "id": "RelateCodeNums3", "disabled": true }).val(RelateCodeNums);
                }
                else {
                    $("#all").attr("checked", true).removeAttr("style");
                    $("#codebatchs").show().find("input").val(CodeBatch).attr("disabled", true);
                    $("#SectionNum").hide();
                    $("label[for='para']").hide();
                    $("label[for='singleCode']").hide();
                    $("#SectionNum2").find("input").attr({"id": "RelateCodeNums2","disabled": true}).val(RelateCodeNums);
                }
            }, 1000)
        },

        Ajax_CodeRelateSave: function () {
            var data = $("#CodeRelateForm").serialize();
            $.L.ajaxHandle({
                url: ACTION_URL("IsCanInsertAndBinding", "CodeManage"),
                data: data,
                type: 'POST',
                success: function (result) {
                    var isInsert=true;
                    if (result == false) {
                        var _confirm = confirm("当前码已经绑定批次，是否重复绑定?")

                        if (_confirm) {

                            isInsert =true

                        } else {
                            isInsert = false
                            _this.Ajax_GetCodeRelatePageList(1, 0);
                            _this.Dialog_CodeRelateAddObj.close();
                            $.L.msgSuccess("已取消！");
                            return;
                        }
                    }
                    if (isInsert==true) {
                        $.L.ajaxHandle({
                            url: ACTION_URL("CodeRelateSave", "CodeManage"),
                            data: data,
                            type: 'POST',
                            success: function (result) {
                                if (result == true) {
                                    _this.Ajax_GetCodeRelatePageList(1, 0);
                                    _this.Dialog_CodeRelateAddObj.close();
                                    $.L.msgSuccess("操作成功！");
                                }
                                else {
                                    $.L.msgWarning("请检查网络");
                                }
                            }
                        });
                    }

                }
            });

        },
        Ajax_CodeDisplaySave: function (obj) {
            var BoxCodes = $("#BoxCodes").val();
            var data = $("#CodeRelateForm").serialize();

            var StartCode = Number($("#StartCode").val());
            var EndCode = Number($("#EndCode").val());
            var HidCount = Number($("#HidCount").val());
            if (StartCode != 0 && EndCode != 0) {
                var num = EndCode - StartCode
                if (num < 0) {
                    $.L.msgError("结束码必须大于起始码！");
                    return;
                }
            }
            if (EndCode > HidCount) {
                $.L.msgError("结束码必须小于整批数量！");
                return;
            }

            

            $.L.ajaxHandle({
                url: ACTION_URL("CodeRelateSave", "CodeManage") + "?BoxCodes=" + BoxCodes ,
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result == true) {
                        _this.Ajax_GetCodeRelatePageList(1);
                        _this.Dialog_CodeDisableAddObj.close();
                        $.L.msgSuccess("操作成功！");
                    }
                    else {
                        $.L.msgWarning(result);                 
                    }
                }
            });
        },
        RelateTypeChange: function (allotType) {
            if (allotType == 0)
            {
                $("#codebatchs").show("fast");
                $("#SectionNum").show("fast");
                $("#single_code").hide("fast");
                $("#StartCode").val("");
                $("#EndCode").val("");
                $("#SectionNum2").find("input").attr("id", "RelateCodeNums1").val("");
            }
            else if (allotType == 2) {
                $("#single_code").show("fast");
                $("#SectionNum").hide("fast");
                $("#codebatchs").hide("fast");
                $("#BoxCodes").val("");
                $("#SectionNum2").find("input").attr("id", "RelateCodeNums3").val("");
            }
            else {
                $("#codebatchs").show("fast");
                $("#single_code").hide("fast");
                $("#SectionNum").hide("fast");
                $("#SectionNum2").find("input").attr("id", "RelateCodeNums2").val("");
                this.ReturnSelectCount(1);
            }
        }
    });
})(jQuery);
